﻿using Dapper;
using DapperExtensions;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Logistics_Dao
{
    
    public class WuliuBusinessDao
    {
        private SqlGeneratorImpl sqlGenerator;
        public WuliuBusinessDao()
        {
            var config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new MySqlDialect());
             sqlGenerator = new SqlGeneratorImpl(config);
            DapperExtensions.DapperExtensions.Configure(config);
        }

        public PageData<DataTable> GetList(ContractAuthorizatQuery predicate, int page, int pageSize)
        {
            var data = new PageData<DataTable>();
            IList<ISort> sort = new List<ISort>
                                    {
                                        Predicates.Sort<wuliu_wlcompany_business>(p => p.id,false)
                                    };
            var sql = @" FROM wuliu_company_apply   LEFT JOIN wuliu_wlcompany_business  on wuliu_company_apply.wuliu_id =wuliu_wlcompany_business.wuliu_id 
            where wuliu_company_apply.apply_month = '" + DateTime.Now.ToString("yyyyMM") + "' and wuliu_wlcompany_business.state=1 and wuliu_company_apply.apply_flag =1 ";
            using (var conn = ConnectionHelper.OpenConnection())
            {
              
               if(predicate.company_id>0)
               {
                   sql += " and  wuliu_company_apply.company_id= " + predicate.company_id;
               }

                var num = conn.ExecuteScalar<int>("select count(1) " + sql);


                sql += " order by wuliu_wlcompany_business.id asc limit @pageIndex,@pageSize";
                DynamicParameters dynamicParameters = new DynamicParameters();
                dynamicParameters.Add("pageIndex", page*pageSize);
                dynamicParameters.Add("pageSize", pageSize);
                //var count = conn.Count<wuliu_bid_load>(predicate);
                var res = conn.Query(" select wuliu_company_apply.id as apply_id,wuliu_company_apply.company_name,wuliu_company_apply.company_id,wuliu_company_apply.company_no,wuliu_wlcompany_business.* " + sql, dynamicParameters);
                data.DataObj = res;
                data.TotalNum = num;
                data.CurrentPage = page;
                data.TotalPageCount = num % pageSize == 0 ? num / pageSize : num / pageSize + 1;
            }
            return data;
        }


        public bool UpdaeFlag(int id, string apply_flag)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                string sql = "Update wuliu_company_apply set apply_flag=@apply_flag where id=@id";
                return conn.Execute(sql, new { apply_flag, id }) == 1;
            }
        }

        private string GetWhereSql(IPredicate predicate, DynamicParameters dynamicParameters)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            var wherSql = predicate.GetSql(sqlGenerator, parameters);

            foreach (var parameter in parameters)
            {
                dynamicParameters.Add(parameter.Key, parameter.Value);
            }
            return wherSql;
        }

        public bool Add(wuliu_wlcompany_business business)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {

                return conn.Insert<wuliu_wlcompany_business>(business)>0;
            }
        }

        public bool Update(wuliu_wlcompany_business business)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Update<wuliu_wlcompany_business>(business) ;
            }
        }



        public wuliu_wlcompany_business GetWLBusinessByWuliuId(int wuliu_id)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                var sql = "select * from wuliu_wlcompany_business where wuliu_id=@wuliu_id and state=1";
                return conn.Query<wuliu_wlcompany_business>(sql, new { wuliu_id }).FirstOrDefault();
 
            }
        }
    }
}
